<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of widget
 *   crea el conjunto de widgets utiles para los campos dinamicos!
 * @author EaDeveloper
 */
class Widget {

    public $widgets = array();
    public $fieldsWidget = array();
    public $db;
    public $rootPath;
    public $widgetsFolder = "widgets/";
    public $registeredTable = false;
    public $tableFields = array();

    public function registerTable($tableName) {
        $this->db = DbManager::getMe();
        $this->tableName = $tableName;
        $sql = sprintf("select * from cllg_tablefield where TABLE_NAME=%s and STATUS=1 order by ORD,FK_TAB", sqlValue($tableName, 'text'));

        $rs = $this->db->queryToArray($sql);
        foreach ($rs as $row) {
            $field = $row["NAME"];
            $this->tableFields[] = array($row["NAME"], $row["WIDGET"]);
            if (!isEmpty($row["WIDGET"])) {
                if ($this->isGridFieldType($field)) {
                    for ($c = 1; $c <= 10; $c++) {
                        $this->register($field . "-" . $c, $row["WIDGET"]);
                    }
                } else {
                    $this->register($field, $row["WIDGET"]);
                }
            }
        }
        $this->registeredTable = true;
    }

    function isGridFieldType($field) {
        $r = $this->db->queryToArray("select t.TIPO from cllg_table_tab t,cllg_tablefield f
		 			where f.NAME='$field' and f.FK_TAB=t.ID and f.TABLE_NAME='$this->tableName'");
        $r = $r[0];
        return ($r["TIPO"] == 1);
    }

    public function register($field, $widget) {
        $fields = isset($this->fieldsWidget[$widget]) ? $this->fieldsWidget[$widget] : array();
        $fields[] = $field;
        $this->fieldsWidget[$widget] = $fields;
    }

    public function includes() {
        $script = array();
        if (count($this->fieldsWidget)>0) {
            foreach ($this->fieldsWidget as $k => $fields) {
                $r = $this->db->queryToArray("select SCRIPT,STYLE from cllg_util_widget where ID=$k");
                $r = $r[0];
?>
                <link href="<?php echo $this->rootPath . $this->widgetsFolder . $r["STYLE"]; ?>" rel="stylesheet" type="text/css" media="screen"  />
                <script src="<?php echo $this->rootPath . $this->widgetsFolder . $r["SCRIPT"]; ?>" type="text/javascript"></script>
<?php
            }
        }
    }

    public function activateWidgets() {

        foreach ($this->fieldsWidget as $k => $fields) {
            $r = $this->db->queryToArray("select * from cllg_util_widget where ID=$k");
            $r = $r[0];
?>
            <script type="text/javascript">
                ( function($){
                    $(document.body).ready(function(){
<?php
            foreach ($fields as $f) {
                $widg = $r['WIDGET'];
                echo "widget.$widg($('#$f'));";
            }
?>
            });
            }
            )(jQuery);
            </script>
<?php
        }
    }

}
?>
